package qc.module.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.module.platform.entity.QcRoleDept;
import qc.module.platform.repository.QcRoleDeptRepository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色与部门关联Service
 *
 * @author QuCheng Tech
 * @since 2023/8/31
 */
@Service
public class RoleDeptService {
    private QcRoleDeptRepository repository;

    @Autowired
    public void setQcRoleDeptRepository(QcRoleDeptRepository repository) {
        this.repository = repository;
    }

    /**
     * 判断关联关系是否存在
     *
     * @param roleId 角色ID
     * @param deptId 部门ID
     * @return boolean
     * @author QuCheng Tech
     * @since 2023/9/2
     */
    public boolean hasExist(int roleId, int deptId) {
        LambdaQueryWrapper<QcRoleDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QcRoleDept::getRoleid, roleId);
        wrapper.eq(QcRoleDept::getDeptid, deptId);

        QcRoleDept en = repository.selectOne(wrapper);
        if (en != null)
            return true;

        return false;
    }

    /**
     * 根据部门ID集合获取关联的角色ID集合
     *
     * @param filterDeptIds 部门ID集合
     * @return java.util.List<java.lang.Integer>
     * @author QuCheng Tech
     * @since 2023/9/2
     */
    public List<Integer> getByDeptIds(List<Integer> filterDeptIds) {
        LambdaQueryWrapper<QcRoleDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(QcRoleDept::getRoleid);
        wrapper.in(QcRoleDept::getDeptid, filterDeptIds);
        //查询数据
        List<QcRoleDept> ens = repository.selectList(wrapper);
        if (ens != null && ens.size() > 0x0) {
            return ens.stream().map(QcRoleDept::getRoleid).collect(Collectors.toList());
        }

        return null;
    }

    /***
     * 根据指定的角色ID关联的部门ID集合
     *
     * @param roleId 角色ID
     * @return java.util.List<java.lang.Integer>
     * @author QuCheng Tech
     * @since 2023/8/31
     */
    public List<Integer> getDeptIds(int roleId) {
        LambdaQueryWrapper<QcRoleDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(QcRoleDept::getDeptid);
        wrapper.eq(QcRoleDept::getRoleid, roleId);
        //查询数据
        List<QcRoleDept> ens = repository.selectList(wrapper);
        if (ens != null && ens.size() > 0x0) {
            return ens.stream().map(QcRoleDept::getDeptid).collect(Collectors.toList());
        }

        return null;
    }

    /**
     * 添加角色部门关联关系
     *
     * @param roleId 角色id
     * @param deptId 部门ID
     */
    public void add(int roleId, int deptId) {
        //判断角色用户是否已经关联如果已经关联则不添加
        if (!hasExist(roleId, deptId)) {
            QcRoleDept en = new QcRoleDept();
            en.setRoleid(roleId);
            en.setDeptid(deptId);
            repository.insert(en);
        }
    }

    /**
     * 删除角色与部门的关联关系
     *
     * @param roleId 角色id
     * @return 成功返回null
     */
    public void deleteByRoleId(int roleId) {
        LambdaUpdateWrapper<QcRoleDept> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(QcRoleDept::getRoleid, roleId);
        repository.delete(wrapper);
    }
}
